package ren.kane.phoenix.util.common;

import com.google.common.base.MoreObjects;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 返回结果类.
 *
 * @author renrk(1275) on 2018-07-16 9:01 PM
 **/
@ApiModel(value = "结果对象", description = "结果对象信息")
public class RestResult<T> {

    @ApiModelProperty(value = "结果码", required = true)
    private String errcode;

    @ApiModelProperty(value = "结果信息", required = true)
    private String errmsg;

    @ApiModelProperty(value = "结果内容")
    private T data;

    public RestResult() {
    }

    public RestResult(String errcode, String errmsg) {
        this.errcode = errcode;
        this.errmsg = errmsg;
    }

    /**
     * 构造方法.
     */
    public RestResult(String errcode, String errmsg, T data) {
        this.errcode = errcode;
        this.errmsg = errmsg;
        this.data = data;
    }

    public String getErrcode() {
        return errcode;
    }

    public void setErrcode(String errcode) {
        this.errcode = errcode;
    }

    public String getErrmsg() {
        return errmsg;
    }

    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 成功.
     */
    public static RestResult success() {
        return success("");
    }

    /**
     * 成功.
     */
    public static <T> RestResult success(T data) {
        RestResult<T> restResult = new RestResult<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
        restResult.setData(data);
        return restResult;
    }

    /**
     * 错误.
     */
    public static RestResult error(ResultEnum resultEnum) {
        return error(resultEnum, "");
    }

    /**
     * 错误.
     */
    public static <T> RestResult error(ResultEnum resultEnum, T data) {
        RestResult<T> restResult = new RestResult<>(resultEnum.getCode(), resultEnum.getMsg());
        restResult.setData(data);
        return restResult;
    }

    public static RestResult error(String errcode, String errmsg) {
        return new RestResult<>(errcode, errmsg, "");
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
            .add("errcode", errcode)
            .add("errmsg", errmsg)
            .add("data", data)
            .toString();
    }
}
